
//基础地址
const url1='http://localhost:5000/'
const url2='http://8.130.37.134/api1/'


//获取鉴权字段处理
function getToken(){
	let userInfo= uni.getStorageSync('userInfo')
	if(userInfo){
		console.log(JSON.parse((userInfo)).token)
		return JSON.parse((userInfo)).token
	}
	return ''
	
}



// 封装请求方法
function request(method, url, data, success, fail) {
  uni.request({
    url: url2+url,
    method: method,
    data: data,
    header: {
      'Content-Type': 'application/json' ,
	  'Authorization':'Bearer '+getToken(),
    },
    success: (res) => {
      if (typeof success === 'function') {
        success(res.data);
      }
    },
    fail: (err) => {
      if (typeof fail === 'function') {
        fail(err);
      }
    }
  });
}

// 封装 GET 请求
function get(url, data, success, fail) {
  request('GET', url, data, success, fail);
}

// 封装 POST 请求
function post(url, data, success, fail) {
  request('POST', url, data, success, fail);
}

// 封装 DELETE 请求
function del(url, data, success, fail) {
  request('DELETE', url, data, success, fail);
}

// 封装 PUT 请求
function put(url, data, success, fail) {
  request('PUT', url, data, success, fail);
}

export { get, post, del, put };